package com.shadowtorcher.TableRolePlayingSystem.controller.WebSocket;

import com.shadowtorcher.TableRolePlayingSystem.bean.Chatlogs;
import com.shadowtorcher.TableRolePlayingSystem.bean.Map;
import com.shadowtorcher.TableRolePlayingSystem.bean.User;
import com.shadowtorcher.TableRolePlayingSystem.dao.ChatlogsDao;
import com.shadowtorcher.TableRolePlayingSystem.dao.MapDao;
import com.shadowtorcher.TableRolePlayingSystem.dao.UserDao;

public class SaveProcessor {

	private static ChatlogsDao chatlogsdao;
	private static UserDao userdao;
	private static MapDao mapdao;

	public static int saveLog(String roomID, String data) {
		int chatID;
		// 获得当前聊天记录表中chatid最大值。
		Chatlogs chatlogs = chatlogsdao.MaxchatID(roomID);
		if (chatlogs != null) {
			chatID = chatlogs.getchatID() + 1;
		} else {
			chatID = 0;
		}
		Chatlogs chatlog = new Chatlogs();
		chatlog.setchatID(chatID);
		chatlog.setRoomID(roomID);
		chatlog.setData(data);
		chatlogsdao.addlogs(chatlog);
		return chatID;
	}
	public static void updateUserData(User user) {
        userdao.updateData(user);
	}
	public static void delUserData(User user) {

		userdao.delData(user.getUserName());
	}

	// user 和 password 可能为 null , 如果不能登录返回 null-----------------获得用户角色信息
	public static User getUserByUserPassword(String userName, String userPassword) {
        return userdao.logIn(userName, userPassword);
	}

	// 获得当前地图/房间数据，如果没有返回 null
	public static Map getMapByMapID(String string) {
        return mapdao.getMapByMapID(string);
	}
	public static void updateMapData(Map map) {
		mapdao.updateMapData(map);
	}

	public static ChatlogsDao getChatlogsdao() {
		return chatlogsdao;
	}

	public static void setChatlogsdao(ChatlogsDao chatlogsdao) {
		SaveProcessor.chatlogsdao = chatlogsdao;
	}

	public static UserDao getUserdao() {
		return userdao;
	}

	public static void setUserdao(UserDao userdao) {
		SaveProcessor.userdao = userdao;
	}
	public static MapDao getMapdao() {
		return mapdao;
	}
	public static void setMapdao(MapDao mapdao) {
		SaveProcessor.mapdao = mapdao;
	}

    public static void createUserdata(String userData, String userName) {
		User user = new User();
		user.setUserData(userData);
		user.setUserName(userName);
		userdao.updateData(user);
	}

    // 根据房间号和房间内序号获取消息, 如果为空返回
    public static Chatlogs getLogByRoomAndSerial(String roomID, int chatID) {
        return chatlogsdao.getLogByRoomAndSerial(roomID, chatID);
    }

    // 根据房间号获取下一条发送的消息的id, 最小 id 是 0
    public static int getCurrentByRoom(String roomID) {
    	int chatid=chatlogsdao.getCurrentByRoom(roomID).getchatID();
    	if(chatid!=0) {
    		return chatid+1;
    	}
    		return 0 ;
    	
    }

	public static void registerNewUser(String name, String password, String data, String key) {
		userdao.registerNewUser(name, password, data, key);
	}

	public static void upDateUserPublicKey(String name,String publicKey){
		userdao.upDateUserPublicKey(name,publicKey);
	}

	public static User getUserByName(String name){
		return userdao.getUserByName(name);
	}

	public static void createMap(String mapName, String ownName) {
		mapdao.cereateMap(mapName, ownName);
	}
}
